package com.bw.paper.mvp.view.news;

import android.Manifest;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.paper.App;
import com.bw.paper.R;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.view.BaseFragment;
import com.bw.paper.mvp.contract.LREContract;
import com.bw.paper.mvp.di.DaggerLREComponent;
import com.bw.paper.mvp.di.LREModules;
import com.bw.paper.mvp.model.HttpCode;
import com.bw.paper.mvp.model.entity.ChannelEntity;
import com.bw.paper.mvp.model.entity.TextBannerEntity;
import com.bw.paper.mvp.model.entity.UserEntity;
import com.bw.paper.mvp.presenter.LREPresenter;
import com.bw.paper.mvp.view.news.adapter.NewsVpAdapter;
import com.bw.paper.mvp.view.news.adapter.TextBannerAdapter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
/**
 * 作业：新闻首页吸顶效果协调者布局完成;
 *      新闻首页列表完成;
 *      Glide图片异步加载图片工具类封装;
 *      LRE逻辑编写
 *      git代码仓库提交代码
 * */
public class NewsFragment extends BaseFragment<LREPresenter> implements
        LREContract.ILREView, OnBannerListener<TextBannerEntity.Values>,
        View.OnClickListener {
    private SlidingTabLayout tabLayout;
    private ViewPager viewPager;
    private ImageView news_more_img;
    private Banner<TextBannerEntity.Values,TextBannerAdapter> news_text_banner;
    private TextBannerAdapter textBannerAdapter;
    private NewsVpAdapter newsVpAdapter;
    private List<Fragment> fragments;

    @Override
    public int bindLayout() {
        return R.layout.fragment_news;
    }

    @Override
    public void initView() {
        tabLayout = f(R.id.news_tab);
        viewPager = f(R.id.news_vp);
        news_more_img = f(R.id.news_more_img);
        news_text_banner = f(R.id.news_text_banner);
        news_more_img.setOnClickListener(this::onClick);
    }

    @Override
    public void initData() {
        fragments = new ArrayList<>();
        //获取顶部文字banner数据源
        List<UserEntity> users = App.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
        boolean flag = users.size() == 0;
        //获取频道数据源
        Map<String, Object> bannerMap = new HashMap<>();
        bannerMap.put("code", HttpCode.TEXTBANNERCODE);
        Map<String, Object> channelMap = new HashMap<>();
        channelMap.put("code", HttpCode.USERCHANNELCODE);
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        channelMap.put("imie", flag ? PhoneUtils.getIMEI() : "");
        channelMap.put("userId",flag?0:users.get(0).user_id);
        p.requestAll(bannerMap,channelMap);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this)).build()
                .injectNewsFragment(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        news_text_banner.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        news_text_banner.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        news_text_banner.destroy();
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof TextBannerEntity){
            //文字banner返回
            TextBannerEntity textBannerEntity = (TextBannerEntity) entity;
//            LogUtils.e("textBannerEntity:"+textBannerEntity.values);
            if (textBannerEntity.getValues() == null) {
                ToastUtils.showShort("文字广告数据丢失");
                return;
            }
            textBannerAdapter = new TextBannerAdapter(textBannerEntity.getValues());
            news_text_banner.setAdapter(textBannerAdapter)
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(new ZoomOutPageTransformer())
                    .setOnBannerListener(this);
        }else if (entity instanceof ChannelEntity){
            //频道列表数据
            //按照频道数量以及频道id初始化fragments
            ChannelEntity channelEntity = (ChannelEntity) entity;
            if (channelEntity.getValues()==null){
                ToastUtils.showShort("无频道数据");
                return;
            }
            for (int i = 0;i < channelEntity.getValues().size();i ++){
                fragments.add(new ContentNewsFragment(channelEntity.getValues().get(i).channel_id));
            }
            newsVpAdapter = new NewsVpAdapter(getFragmentManager(),fragments,channelEntity.getValues());
            viewPager.setAdapter(newsVpAdapter);
            tabLayout.setViewPager(viewPager);
        }
    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {

    }

    @Override
    public void OnBannerClick(TextBannerEntity.Values data, int position) {
        ToastUtils.showShort("点击广告:"+data.news_title);
    }

    @Override
    public void onClick(View v) {
        startPage(ChannelActivity.class);
    }
}
